/**
 * 
 */
package org.whereiswally.net.url.generation.imp;

import java.io.Serializable;

import org.whereiswally.net.url.generation.URLGenerator;

/**
 * @author LGIAVEDO
 * 
 */
public class URIContinuousGenerator implements URLGenerator, Serializable {

    private char[] ini = new char[] { 'A', 'A', 'A', 'A', 'A', 'A' };
    private char[] fin = new char[] { 'Z', 'Z', 'Z', 'Z', 'Z', 'Z' };
    private int pos = ini.length - 1;

    public URIContinuousGenerator() {
	super();
    }

    public URIContinuousGenerator(String ini, String fin) {
	super();
	this.ini = ini.toUpperCase().toCharArray();
	this.fin = fin.toUpperCase().toCharArray();
	pos = this.ini.length - 1;
    }

    public URIContinuousGenerator(int size) {
	super();
	char[] tmp = new char[size];
	for (int i = 0; i < tmp.length; i++) {
	    tmp[i] = 'A';
	}
	this.ini = tmp;
	char[] tmp2 = new char[size];
	for (int i = 0; i < tmp2.length; i++) {
	    tmp2[i] = 'Z';
	}
	this.fin = tmp2;
	pos = this.ini.length - 1;
    }

    /*
     * @see org.whereiswally.net.url.generation.URLGenerator#next()
     */
    @Override
    public String next() {
	if (ini[pos] != 'Z')
	    ini[pos]++;
	else {
	    while (pos >= 0) {
		pos--;
		if (ini[pos] <= 'Z') {
		    ini[pos]++;
		    for (int i = pos + 1; i < ini.length; i++) {
			ini[i] = 'A';
		    }
		    pos = ini.length - 1;
		    break;
		}
	    }

	}
	return new String(ini);
    }

    /*
     * @see org.whereiswally.net.url.generation.URLGenerator#hasNext()
     */
    @Override
    public boolean hasNext() {
	if (new String(ini).equals(new String(fin)))
	    return false;
	return true;
    }

    public static void main(String a[]) {
	URIContinuousGenerator uGenerator = new URIContinuousGenerator(4);
	while (uGenerator.hasNext()) {
	    System.out.println(uGenerator.next());
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.whereiswally.net.url.generation.URLGenerator#getLastGenerated()
     */
    @Override
    public String getLastGenerated() {
	return new String(ini);
    }

}
